package com.withoutwords.ui.custom;

import java.util.ArrayList;

import com.withoutwords.R;
import com.withoutwords.engine.Game;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

public class GamesExpandableListAdapter extends BaseExpandableListAdapter {

	ArrayList<Game> games = new ArrayList<Game>();
	
	Handler joinGameHandler = null;
	  private LayoutInflater mInflater;
	Context context;

	public GamesExpandableListAdapter(Context contxt,Handler handler) {
		this.context = contxt;
		 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 joinGameHandler = handler;
	}

	public void setGames(ArrayList<Game> newGameSet) {
		games = newGameSet;
	}
	public Game getGameAt(int index) {
		return games.get(index);
	}


	public Object getChild(int groupPosition, int childPosition) {
		return games.get(groupPosition).getPlayer(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return games.get(groupPosition).getPlayersCount();
	}

	public TextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				AbsListView.LayoutParams.MATCH_PARENT, 64);

		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		 
		TextView textView = getGenericView();
		textView.setText(getChild(groupPosition, childPosition).toString());
		return textView;
	}

	public Object getGroup(int groupPosition) {
		return games.get(groupPosition);
	}

	public int getGroupCount() {
		return games.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		//ViewGroup vg = null;
		 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.game_list_group, parent, false);
         } 		//LayoutInflater inf  = context.
		TextView textView =(TextView)convertView.findViewById(R.id.txtGameOwnerName);
		textView.setText(getGroup(groupPosition).toString());
		Button btnJoin  = (Button)convertView.findViewById(R.id.btnJoinGame);
		
		btnJoin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle data = new Bundle();
				data.putSerializable("game", games.get(groupPosition));
				msg.setData(data);
				
				joinGameHandler.sendMessage(msg);
				
			}
		});
		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	public boolean hasStableIds() {
		return true;
	}

}
